package com.atguigu.eduorder.controller;


import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.R;
import com.atguigu.eduorder.client.EduClient;
import com.atguigu.eduorder.entity.Order;
import com.atguigu.eduorder.entity.vo.timestampVo;
import com.atguigu.eduorder.service.OrderService;
import com.atguigu.handler.GuiliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-11-03
 */
@Controller
@RequestMapping("/eduorder/order")

public class OrderController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private EduClient eduClient;

    //1 生成订单的方法
    @PostMapping("createOrder/{courseId}")
    @ResponseBody
    public R saveOrder(@PathVariable String courseId, HttpServletRequest request) {
        //创建订单，返回订单号
        String orderNo = orderService.createOrders(courseId, JwtUtils.getMemberIdByJwtToken(request));
        return R.ok().data("orderId",orderNo);
    }

    //2 根据订单id查询订单信息
    @RequestMapping("getOrderInfo/{orderId}")
    @ResponseBody
    public R getOrderInfo(@PathVariable String orderId) {
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("order_no",orderId);
        Order order = orderService.getOne(wrapper);
        return R.ok().data("item",order);
    }
    //根据课程id和用户id查询订单表中订单状态---供edu_course调用
    @RequestMapping("isBuyCourse/{courseId}/{memberId}")
    @ResponseBody
    public boolean isBuyCourse(@PathVariable String courseId,@PathVariable String memberId) {
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        wrapper.eq("member_id",memberId);
        wrapper.eq("status",1);//支付状态 1代表已经支付
        int count = orderService.count(wrapper);
        if(count>0) { //已经支付
            return true;
        } else {
            return false;
        }
    }

    //查询订单表中订单的状态 判断课程是否购买
    @ApiOperation( "判断课程是否购买")
    @RequestMapping("isBought/{courseId}")
    @ResponseBody
    public R isBuyByCourseId(@PathVariable String courseId, HttpServletRequest request) {
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        int count = orderService.getonestatus(courseId, memberId);
        if (count>0){
            return R.ok().data("isBought", true);
        }
        return R.ok().data("isBought", false);
    }

    //获取当前用户订单列表
    @ApiOperation(value = "获取当前用户订单列表")
    @RequestMapping("orderList")
    @ResponseBody
    public R list(HttpServletRequest request) {
        String jwtToken = JwtUtils.getMemberIdByJwtToken(request);
        List<Order> list = orderService.selectByMemberId(jwtToken);
        return R.ok().data("items", list);
    }

    @ApiOperation(value = "删除订单")
    @RequestMapping("removeOrder/{orderId}")
    @ResponseBody
    public R remove(@PathVariable String orderId, HttpServletRequest request) {
        String jwtInfo = JwtUtils.getMemberIdByJwtToken(request);
        boolean result = orderService.removeById(orderId, jwtInfo);
        if(result){
            return R.ok().message("删除订单成功");
        }else{
            return R.error().message("数据不存在");
        }
    }


    //支付成功处理
    @RequestMapping("payok")
    public String payok(@RequestParam("out_trade_no") String out_trade_no,HttpServletRequest request){
        Order order = orderService.getOne(new QueryWrapper<Order>().eq("order_no", out_trade_no));
        order.setStatus(1);
        order.setPayType(2);
        orderService.updateById(order);
        R res = eduClient.getinfo("第一个课程",order.getMemberId());
        if (res.getSuccess()){
            timestampVo timestampVo = new timestampVo();
            String content="第一次购买了 【"+order.getCourseTitle()+"】 课程，祝您在学习技术的道路上越走越远";
            timestampVo.setContent(content);
            timestampVo.setUserid(order.getMemberId());
            timestampVo.setTime(new Date());
            timestampVo.setTitle("第一个课程");
            eduClient.saveTs(timestampVo);
        }
        String url="http://localhost:3000/course/"+order.getCourseId();
        return "redirect:" + url;
    }


}

